package com.huashi.BinaryTree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 广度优先遍历
 */
public class BFS {
    public int[] levelOrder(TreeNode root){
        ArrayList<Integer> result = new ArrayList<>();
        ArrayList<TreeNode> tree = new ArrayList<>();

        //放入根节点
        if (root == null)
            return new int[0];
        tree.add(root);

        while (!tree.isEmpty()){
            root = tree.get(0);
            tree.remove(0);
            result.add(root.val);
            //依次将每层节点放入
            if (root.left!=null){
                tree.add(root.left);
            }
            if (root.right!=null){
                tree.add(root.right);
            }
        }

        //存储
        int size = result.size();
        int[] R = new int[size];
        for (int i=0;i<size;i++){
            R[i] = result.get(i);
        }
        return R;
    }

}
